package com.waverider.promotion.biz.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sun.misc.BASE64Decoder;

public class TaoBaoUtils {
	private static Log logger = LogFactory.getLog(TaoBaoUtils.class);

	public static Map<String, String> convertBase64StringtoMap(String str) {
		BASE64Decoder decoder = new BASE64Decoder();

		if (str == null)

			return null;

		String keyvalues = null;

		try {

			keyvalues = new String(decoder.decodeBuffer(str), "gbk");

		} catch (UnsupportedEncodingException e) {

			logger.error("str=" + str, e);

		} catch (IOException e) {
			logger.error("str=" + str, e);
		}

		String[] keyvalueArray = keyvalues.split("\\&");

		Map<String, String> map = new HashMap<String, String>();

		for (String keyvalue : keyvalueArray) {

			String[] s = keyvalue.split("\\=");

			if (s == null || s.length != 2)

				return null;

			map.put(s[0], s[1]);

		}

		return map;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out
				.println(TaoBaoUtils
						.convertBase64StringtoMap("aWZyYW1lPTEmdHM9MTMxODUwODUwMjE3MyZ2aXNpdG9yX2lkPTUyODY1OTY3MCZ2aXNpdG9yX25pY2s9zNSxpr+qt8XGvcyo"));
	}
}
